package com.tgy.MyFlowable.workflow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctsi.core.common.entity.LoginUser;
import com.ctsi.core.common.exception.MarsException;
import com.ctsi.core.common.util.$;
import com.ctsi.core.common.util.SecurityUtil;
import com.ctsi.core.common.util.StringUtil;
import com.ctsi.core.database.entity.Search;
import com.ctsi.core.database.util.PageUtil;
import com.ctsi.core.web.enums.YesOrNotEnum;
import com.ctsi.workflow.dto.FlwOptionDTO;
import com.ctsi.workflow.entity.FlwOption;
import com.ctsi.workflow.enums.FlwOptionExceptionEnum;
import com.ctsi.workflow.mapper.FlwOptionMapper;
import com.ctsi.workflow.service.IFlwDefinitionService;
import com.ctsi.workflow.service.IFlwOptionService;
import com.ctsi.workflow.util.BpmTitleUtil;
import org.flowable.engine.repository.ProcessDefinition;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 流程选项配置表 服务实现类
 *
 * @version 1.0
 * @author: wang xiao xiang
 * @date: 2021/8/8 19:45
 */
@Service
public class FlwOptionServiceImpl extends ServiceImpl<FlwOptionMapper, FlwOption> implements IFlwOptionService {

    /**
     * 默认流程标题规则
     */
    private static final String DEFAULT_FLW_TITLE_REG = "user在time发起了title";

    @Resource
    private IFlwDefinitionService flwDefinitionService;

    @Resource
    private HttpServletRequest request;

    @Override
    public IPage<FlwOption> listPage(Search search) {
        LambdaQueryWrapper<FlwOption> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtil.isNotBlank(search.getKeyword())) {
            queryWrapper.like(FlwOption::getId, search.getKeyword());
        }
        queryWrapper.orderByDesc(FlwOption::getCreateTime);
        return this.baseMapper.selectPage(PageUtil.getPage(search), queryWrapper);
    }

    @Override
    public void addByProcessDefinition(ProcessDefinition processDefinition) {
        FlwOption flowableOption = new FlwOption();
        flowableOption.setProcessDefinitionId(processDefinition.getId());
        //默认自动完成第一个任务
        flowableOption.setJumpFirst(YesOrNotEnum.Y.getCode());
        //默认跳过相同处理人
        flowableOption.setSmartComplete(YesOrNotEnum.Y.getCode());
        this.save(flowableOption);
    }

    @Override
    public List<FlwOption> list(FlwOptionDTO flwOptionDTO) {
        return this.list(Wrappers.<FlwOption>lambdaQuery()
                .eq($.isNotBlank(flwOptionDTO.getProcessDefinitionId()),
                        FlwOption::getProcessDefinitionId, flwOptionDTO.getProcessDefinitionId()));
    }

    @Override
    public void delete(String id) {
        this.remove(Wrappers.<FlwOption>lambdaQuery().eq(FlwOption::getProcessDefinitionId, id));
    }

    @Override
    public String getTitleByProcessDefinitionId(String processDefinitionId) {
        ProcessDefinition processDefinition = flwDefinitionService.queryProcessDefinition(processDefinitionId);
        FlwOption flwOption = this.getOne(Wrappers.<FlwOption>lambdaQuery().eq(FlwOption::getProcessDefinitionId, processDefinitionId));
        if ($.isNull(flwOption)) {
            throw new MarsException(FlwOptionExceptionEnum.OPTION_NOT_EXIST);
        }
        String titleReg = flwOption.getTitle();
        LoginUser username = SecurityUtil.getUsername(request);
        //如果设置了标题规则，则处理标题
        if ($.isNotEmpty(titleReg)) {
            return BpmTitleUtil.formatTitle(titleReg, processDefinition.getName(), username.getAccount());
        } else {
            //如果没设置标题规则，则使用默认的
            return BpmTitleUtil.formatTitle(DEFAULT_FLW_TITLE_REG, processDefinition.getName(), username.getAccount());
        }
    }

    @Override
    public boolean getFlowableOptionJumpFirst(String processDefinitionId) {
        FlwOption flwOption = this.getOne(Wrappers.<FlwOption>lambdaQuery().eq(FlwOption::getProcessDefinitionId, processDefinitionId));
        if ($.isNull(flwOption)) {
            throw new MarsException(FlwOptionExceptionEnum.OPTION_NOT_EXIST);
        }
        return YesOrNotEnum.Y.getCode().equals(flwOption.getJumpFirst());
    }

    @Override
    public boolean getFlowableOptionSmartComplete(String processDefinitionId) {
        FlwOption flwOption = this.getOne(Wrappers.<FlwOption>lambdaQuery().eq(FlwOption::getProcessDefinitionId, processDefinitionId));
        if ($.isNull(flwOption)) {
            throw new MarsException(FlwOptionExceptionEnum.OPTION_NOT_EXIST);
        }
        return YesOrNotEnum.Y.getCode().equals(flwOption.getSmartComplete());
    }
}
